package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Grade;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.IGradeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("grade")
public class GradeController {
    @Autowired
    private IGradeService gradeService;

    /*创建一个方法来确定*/
    @RequestMapping("index")
    public String index(){
        //查找到并共享
        return "grade/list";
    }
    @RequiresPermissions("grade:list")
    @PermissionName("班级管理列表")
    @ResponseBody
    @RequestMapping("list")
    public PageResult list(Model model, @ModelAttribute("qo")QueryForObject qo){
        return gradeService.query(qo);
    }

    /**
     *
     * @param grade 保存班级管理
     * @return 返回json
     */
    @RequiresPermissions("grade:save")
    @PermissionName("班级管理保存")
    @ResponseBody
    @RequestMapping("save")
    public JsonResult save(Grade grade){
        JsonResult result = new JsonResult();
        try {
            gradeService.insert(grade);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存失败");
        }
        return result;
    }
    @RequiresPermissions("grade:update")
    @PermissionName("班级管理更新")
    @ResponseBody
    @RequestMapping("update")
    public JsonResult update(Grade grade){
        JsonResult result = new JsonResult();
        try {
            gradeService.updateByPrimaryKey(grade);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新失败");
        }
        return result;
    }
    //删除班级管理
    @RequiresPermissions("grade:changeSate")
    @PermissionName("改变教室状态")
    @ResponseBody
    @RequestMapping("changeSate")
    public JsonResult changeState(Long id){
        JsonResult result = new JsonResult();
        try {
            gradeService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新失败");
        }
        return result;
    }

    //获得所有班级的List集合 //课程表查询也需要
    @RequiresPermissions("grade:queryAllGrade")
    @PermissionName("获得所有班级")
    @ResponseBody
    @RequestMapping("queryAllGrade")
    public List<Grade> queryAllGrade(){
        return gradeService.selectAll();
    }




}
